package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.tabhome

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.hitPointCameroon
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.me.MeCameroonActivity
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.subview.HotlineCameroonDialog
import com.helasasa.maxfunds.lineofcredit.cashloan.databinding.ActivityCameroonTabHomeBinding

class TabHomeCameroonActivity : AppCompatActivity() {

    private lateinit var tabHomeViewModel: TabHomeCameroonViewModel
    private lateinit var binding: ActivityCameroonTabHomeBinding

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 数据模型
        tabHomeViewModel = ViewModelProvider(this)[TabHomeCameroonViewModel::class.java]

        // 关联布局
        binding = ActivityCameroonTabHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 用到的控件
        val back = binding.navbarLeft
        val hotline = binding.navbarRight
        val loading = binding.loading
        val amount = binding.amount
        val submit = binding.submit
        val tabMe = binding.tabMe

        // 页面loading状态接口监听
        tabHomeViewModel.loadingResult.observe(this@TabHomeCameroonActivity, Observer {
            val loadingResult = it ?: return@Observer

            if (loadingResult) {
                loading.visibility = View.VISIBLE
            } else {
                loading.visibility = View.GONE
            }
        })

        // 页面详情接口监听
        tabHomeViewModel.pageDetailResult.observe(this@TabHomeCameroonActivity, Observer {
            val pageDetailResult = it ?: return@Observer

            loading.visibility = View.GONE
            if (pageDetailResult.error != null) {
                showMessage(pageDetailResult.error)
            }
            if (pageDetailResult.success != null) {
                amount.text = pageDetailResult.success.optString("homeAmount")
            }
        })

        // 流程接口监听
        tabHomeViewModel.mainProcessResult.observe(this@TabHomeCameroonActivity, Observer {
            val mainProcessResult = it ?: return@Observer

            loading.visibility = View.GONE
            if (mainProcessResult.error != null) {
                showMessage(mainProcessResult.error)
            }

            if (mainProcessResult.success != null) {
                tabHomeViewModel.nextRouter(mainProcessResult.success)
            }
        })

        // 页面需要跳转监听
        tabHomeViewModel.nextRouterResult.observe(this@TabHomeCameroonActivity, Observer {
            val nextRouterResult = it ?: return@Observer

            loading.visibility = View.GONE
            val intent = Intent(this@TabHomeCameroonActivity, nextRouterResult.nextPage)
            startActivity(intent)
        })

        // 点击返回
        back.setOnClickListener {
            finish()
        }

        // 点击客服
        hotline.setOnClickListener {
            // 客服弹框
            HotlineCameroonDialog().showNow(supportFragmentManager, "hotline")
            hitPointCameroon("SHOW_HOTLINE")
        }

        // 点击申请
        submit.setOnClickListener {
            hitPointCameroon("TAB_HOME_APPLY_CLICK")
            tabHomeViewModel.startApply()
        }

        // 点击 loan
        tabMe.setOnClickListener {
            val intent = Intent(this@TabHomeCameroonActivity, MeCameroonActivity::class.java)
            startActivity(intent)
        }

        // 查询页面详情
        tabHomeViewModel.pageDetail()
    }

    fun showMessage(message: String) {
        Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
    }

    override fun onResume() {
        super.onResume()
        hitPointCameroon(type = this::class.java.simpleName, isPageStart = true)
    }

    override fun onPause() {
        super.onPause()
        hitPointCameroon(type = this::class.java.simpleName, isPageEnd = true)
    }
}